﻿using System;
using System.Configuration;

namespace uTILLIty.ADExtractor.Core.Formatters
{
	public class EnumFormatter<TEnum> : IFormatter
	{
		public string AttributeName { get; protected set; }

		public EnumFormatter(string attributeName)
		{
			AttributeName = attributeName;
			if (!typeof(TEnum).IsEnum)
				throw new ConfigurationErrorsException(string.Format("EnumT '{0}' must be an ENUM type", typeof(TEnum)));
		}

		public string Format(string attributeName, object value, string valueSeparator)
		{
			var enumValue = (TEnum)value;
			var text = enumValue.ToString();
			if (!string.IsNullOrWhiteSpace(valueSeparator))
				text = text.Replace(", ", valueSeparator);

			return text;
		}

		public bool CanFormat(string attributeName)
		{
			return AttributeName.Equals(attributeName, StringComparison.InvariantCultureIgnoreCase);
		}
	}
}